//
//  ProductViewController.m
//  RoadLine
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 RedFireNoodles. All rights reserved.
//

#import "ProductViewController.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
#import "Singleton.h"

@interface ProductViewController ()<UIWebViewDelegate>
@property(nonatomic, retain)UIWebView *webView;
@property(nonatomic, retain)UIActivityIndicatorView *activityIndicatorView;

@end

@implementation ProductViewController
//自定义初始化方法:
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"特  价";
    }
    return self;
}
// 数据解析
-(void)analysisLoading
{
    // 创建网络请求
    NSURL *url = [NSURL URLWithString:@"http://breadtrip.com/tp/list/"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 建立webView
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49-64)];
    self.webView.backgroundColor = [UIColor whiteColor];
    // 签订webView的代理
    self.webView.delegate = self;
    // 是否伸缩内容适应屏幕:
    self.webView.scalesPageToFit = NO;
    
    // 加载网络请求:
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
    
    [_webView release];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    
    //调用解析方法
    [self analysisLoading];
    
    // 左上角边返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-pullleft"] style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemBack)];
    
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    // 右上角刷新方法
    UIBarButtonItem *buttonReload = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-shuaxin"] style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemreFresh)];
    buttonReload.tintColor = [UIColor whiteColor];
    
    //右上角前进方法
     UIBarButtonItem *buttonAdvance = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-pullright"] style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemAdvance)];
    buttonAdvance.tintColor = [UIColor whiteColor];
    // 加入数组实现多个按钮
    self.navigationItem.rightBarButtonItems = @[buttonAdvance,buttonReload];
    
    //修改UInavigationBar的字体颜色(系统API没有这个设置 只能自己建一个Label覆盖 然后修改label的属性)
    UILabel *titleLabelPrice = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabelPrice.font = [UIFont boldSystemFontOfSize:18];
    titleLabelPrice.textColor = [UIColor whiteColor];
    titleLabelPrice.backgroundColor = [UIColor clearColor];
    titleLabelPrice.text = @"      特价";
    self.navigationItem.titleView = titleLabelPrice;
    [titleLabelPrice release];
    
    if ([Singleton shareSingleton].isNight) {
        [[Singleton shareSingleton] loadNightModeInfoFormDisk];
    }
    
    // 内存管理
    [buttonAdvance release];
    [buttonReload release];
}

- (void)setLightDayModel {
    [self.webView stringByEvaluatingJavaScriptFromString:@" document.body.style.background = '#F9F2E2' "];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:246/255.0 green:85/255.0 blue:91/255.0 alpha:1];
}

- (void)setNightDayModel {
     [self.webView stringByEvaluatingJavaScriptFromString:@" document.body.style.background = '#CCCCCC' "];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:4/255.0 green:165/255.0 blue:171/255.0 alpha:1];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // 下一个详细画面的js
//    [self.webView stringByEvaluatingJavaScriptFromString:@"        var bodyChilds = document.body.childNodes; var divChilds = bodyChilds[1].childNodes; if (!(bodyChilds.length >= 20 && bodyChilds.length <= 26)) { divChilds[1].style.display = 'none'; var intro = document.getElementsByTagName('h4'); for (var i = 0;i < intro.length;i++) { if (intro[i].innerHTML == '面包推荐理由') { intro[i].innerHTML = '推荐理由';} } }         var comments = document.getElementById('comments'); comments.style.display = 'none';"];
    [self.webView stringByEvaluatingJavaScriptFromString:@" var comments = document.getElementById('comments'); comments.style.display = 'none'; "];
    if ([Singleton shareSingleton].isNight) {
        [[Singleton shareSingleton] loadNightModeInfoFormDisk];
    }
}

// 返回
- (void)barButtonItemBack
{
    [self.webView goBack];
    if ([Singleton shareSingleton].isNight) {
        [[Singleton shareSingleton] loadNightModeInfoFormDisk];
    }
}
// 前进
- (void)barButtonItemAdvance
{
    [self.webView goForward];
    if ([Singleton shareSingleton].isNight) {
        [[Singleton shareSingleton] loadNightModeInfoFormDisk];
    }
}
// 刷新
- (void)barButtonItemreFresh
{
    [self.webView reload];
    if ([Singleton shareSingleton].isNight) {
        [[Singleton shareSingleton] loadNightModeInfoFormDisk];
    }
}
// 内存管理:
- (void)dealloc
{
    [_webView release];
    [_activityIndicatorView release];
    [super dealloc];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
